//
//  KeyValuesClass.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/8/24.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "KeyValuesClass.h"
#import "EditModel.h"
#import "KeyNameClass.h"
#import "EducationAdminClient_URL.h"
#import "NetRequest.h"


#define EQUAL @" == "
#define BUDENGYU @" != "
#define DAYU @" > "
#define XIAOYU @" < "

@implementation KeyValuesClass



-(void)setHiddens:(NSMutableArray *)hiddens andSendDic:(NSMutableDictionary *)sendDic andMana:(NSMutableArray *)mana andReturnBlock:(void (^)(NSMutableDictionary *, NSMutableArray *))cb{
    
    _hiddenArr = hiddens;
    _sendMutDic = sendDic;
    _manyManyArr = mana;
    
    for (EditModel *model in _hiddenArr) {
        NSString *fieldRole = [NSString stringWithFormat:@"%@", model.fieldRole]; //[dic objectForKey:@"fieldRole"];
        
        NSInteger num =[fieldRole integerValue];
        if (num == 16) {
            [self setHiddenDataDic:model];
        }else{
            [self setHiddenNormal:model];
        }
    }
    
    cb(_sendMutDic, _manyManyArr);
    
}

#pragma mark - setHiddenDataDic 隐藏字段区
-(void)setHiddenDataDic:(EditModel *)model{
    
    NSString *dicDefaultSelectStr = [NSString stringWithFormat:@"%@", model.dicDefaultSelect];
    //    NSString *fieldRole= [NSString stringWithFormat:@"%@", model.fieldRole]; //[dic objectForKey:@"fieldRole"];
    //    NSString *fieldId= [NSString stringWithFormat:@"%@", model.fieldId]; //[dic objectForKey:@"fieldId"];
    //    NSString *pageId= [_ADDBTN objectForKey:@"startTurnPage"];
    
#warning ttableId may not this
    //    NSString *tableId= [_ADDBTN objectForKey:@"tableId"];
    NSString *baseString = model.montageName;
    [_sendMutDic setObject:dicDefaultSelectStr forKey:baseString];
    
    //*** receive show
    
    NSLog(@"%@",_sendMutDic);
    
    
}

#pragma mark - 平常字段设置隐藏字段
-(void)setHiddenNormal:(EditModel *)model{
    
    NSString *dicDefaultSelectStr = [NSString stringWithFormat:@"%@", model.true_defaultShowValName];
    //    NSString *fieldRole= [NSString stringWithFormat:@"%@", model.fieldRole]; //[dic objectForKey:@"fieldRole"];
    //    NSString *fieldId= [NSString stringWithFormat:@"%@", model.fieldId]; //[dic objectForKey:@"fieldId"];
    //    NSString *pageId= [_ADDBTN objectForKey:@"startTurnPage"];
    
#warning ttableId may not this
    //    NSString *tableId= [_ADDBTN objectForKey:@"tableId"];
    NSString *baseString = model.montageName;
    [_sendMutDic setObject:dicDefaultSelectStr forKey:baseString];
    
    //*** receive show
    
    NSLog(@"%@",_sendMutDic);
    
    
}

#pragma mark - 前提条件提取
-(NSString *)textQianTiTiaoJian:(EditModel *)model andTstring:(NSString *)tString andFieldArr:(NSMutableArray *)fieldArr{
    //    NSString *jsWhereStr = [NSString stringWithFormat:@"%@", [dic valueForKey:@"jsWhereStr"]]; //前提条件字符串
    NSString *jsWhereStr = [NSString stringWithFormat:@"%@", model.jsWhereStr]; //前提条件字符串
    
    
    NSString *componentString;
    
    if([jsWhereStr rangeOfString:EQUAL].location != NSNotFound){ // 等于的情况下
        componentString = EQUAL;
        
    }else if([jsWhereStr rangeOfString:BUDENGYU].location != NSNotFound){ // 不等于的情况下
        componentString = BUDENGYU;
    }else if([jsWhereStr rangeOfString:DAYU].location != NSNotFound){
        componentString = DAYU;
        
    }
    NSArray *arrDev = [jsWhereStr componentsSeparatedByString:componentString];                        //前提条件分割键值
    
    NSMutableString *arrDevSMut;
    if(arrDev[0])arrDevSMut= [NSMutableString stringWithFormat:@"%@", arrDev[0]];              //防止空字符串
    NSRange range1 = [arrDevSMut rangeOfString:@"#{"]; //将无用的#{去掉
    if (range1.location != NSNotFound) {
        [arrDevSMut replaceCharactersInRange:range1 withString:@""];
    }
    
    NSRange range2 = [arrDevSMut rangeOfString:@"}"]; //将无用的}去掉
    if (range2.location != NSNotFound) {
        [arrDevSMut replaceCharactersInRange:range2 withString:@""];
    }
    
    NSArray *arrDevId = [arrDevSMut componentsSeparatedByString:@":"]; // 分离默认Id
    NSString *fieldIdQt;
    if(arrDevId.count>1)fieldIdQt = [NSString stringWithFormat:@"%@", arrDevId[1]];
    if (fieldIdQt) {
        NSString *settedKeyString = [self dataDicKeyGet:nil orDefaultId:fieldIdQt andTstring:tString];//看是否有设置
        NSString *settedValueString = [_sendMutDic valueForKey:settedKeyString];//无返回
        
        
        NSString *strLeftName;
        for (EditModel *dicModel in fieldArr) {
            
            NSString *fieldIdQtF = [NSString stringWithFormat:@"%@", dicModel.fieldId];
            
            if ([fieldIdQtF isEqualToString:fieldIdQt]) {
                strLeftName = model.fieldCnName;
                
            }
        }
        if (![settedValueString isEqualToString:arrDev[1]]) {
            if([componentString isEqualToString:EQUAL]){
                
                return strLeftName;//不是需要的前提条件 等于什么的情况下 前提条件
            }else{
                return @"YES"; // 不等于什么的情况下 前提条件
            }
        }else{
            
            if([componentString isEqualToString:EQUAL]){
                
                return @"YES";//是需要的前提条件
            }else{
                return strLeftName;
            }
        }
    }
    return @"YES";//默认返回YES
    
}

#pragma mark - 数据字典拼接获取
-(NSString *)dataDicKeyGet:(EditModel *)model orDefaultId:(NSString *)fieldId andTstring:(NSString *)tString{
    
    if(fieldId){
        
    }else{
        
        fieldId= [NSString stringWithFormat:@"%@", model.fieldId]; //[dic objectForKey:@"fieldId"];
    }
    NSString *pageId= [_EditDic objectForKey:@"startTurnPage"];
    NSString *tableId= [_EditDic objectForKey:@"tableId"];
    
    NSString *fieldRole= [NSString stringWithFormat:@"%@", @"16"];//[dic objectForKey:@"fieldRole"];
    NSString *baseString = [NSString stringWithFormat:@"_au_%@_%@_%@",tableId,pageId,fieldId];
    
    NSString *returnStr = [KeyNameClass getKeyName:fieldRole andBaseString:baseString andTUseString:tString];
    return returnStr;
}


-(NSString *)makeTheBaseString:(EditModel *)model andTheEditDic:(NSMutableDictionary *)editDic{
    
    _EditDic = editDic;
    //    NSString *fieldRole= model.fieldRole;
    //    NSString *fieldId= [dic objectForKey:@"fieldId"];
    NSString *pageId= [_EditDic objectForKey:@"startTurnPage"];
    
    
    NSString *fieldId;
    if(model.relationTableId!=nil){
        if(model.dialogField){
            
            fieldId= model.dialogField;
        }else{
            fieldId= model.fieldId;
        }
    }else{
        fieldId= model.fieldId;
        
    }
    //*** change the tableId
    NSString *tableId= [_EditDic objectForKey:@"tableId"];
    NSString *baseString = [NSString stringWithFormat:@"_au_%@_%@_%@",tableId,pageId,fieldId];
    
    return baseString;
}

#pragma mark - 数据字典多值选择项封装值拼接
-(void)alertOneViewUse:(NSMutableDictionary *)sendMutDic  andModel:(EditModel *)model andValues:(id)values{
    _sendMutDic = sendMutDic;
    [_sendMutDic setObject:values forKey:model.montageName];
    
}

#pragma mark - 弹出层键拼接
-(NSString *)getAlertKKey:(EditModel *)model andTstring:(NSString *)tString{
    
    NSString *fieldRole= model.fieldRole;
    
    //    NSString *fieldId;
    if ([[NSString stringWithFormat:@"%@", model.fieldId]isEqualToString:@"630"]) {
        
    }
    
#warning maybe difernt tableId to use it for a moment
    
    NSString *baseString = [self makeTheBaseString:model andTheEditDic:_EditDic];
    
    
    //    NSString *fieldOfNum;
    
    NSString *keyString = [KeyNameClass getKeyName:fieldRole andBaseString:baseString andTUseString:tString];
    
    return keyString;
}

#pragma mark - 多选下弹层fieldOfNum
-(NSString *)getAlertMoreKKeyFieldOfNum:(EditModel *)model andTstring:(NSString *)tString{
    
    NSString *usageFieldId = model.fieldId;
    
    NSString *relationTableId=[NSString stringWithFormat:@"%@",model.relationTableId] ;
    NSString *showFieldArr=[NSString stringWithFormat:@"%@",model.showFieldArr] ;
    NSString *fieldId;
    if(model.relationTableId!=nil){
        if(model.dialogField){
            
            fieldId= model.dialogField;
        }else{
            fieldId= model.fieldId;
        }
    }else{
        fieldId= model.fieldId;
        
    }
    
    NSString *fieldOfNum;
    if(usageFieldId){
        
        fieldOfNum=[NSString stringWithFormat:@"%@_au_%@_%@_%@_dz", tString,relationTableId,showFieldArr,usageFieldId];
    }else{
        fieldOfNum=[NSString stringWithFormat:@"%@_au_%@_%@_%@_dz", tString,relationTableId,showFieldArr,fieldId];
    }
    
    return fieldOfNum;
}

#pragma mark - 多选下弹层键fieldOfValue
-(NSString *)getAlertMoreKKeyFieldOfValue:(EditModel *)model andNum:(NSInteger)_tNum{
    
    NSString *fieldId;
    
    if(model.relationTableId!=nil){
        if(model.dialogField){
            
            fieldId= [NSString stringWithFormat:@"%@", model.dialogField];
        }else{
            fieldId= [NSString stringWithFormat:@"%@", model.fieldId];
        }
    }else{
        fieldId= [NSString stringWithFormat:@"%@", model.fieldId];
    }
    
    NSString *showFieldArr=[NSString stringWithFormat:@"%@",model.showFieldArr] ;
    NSString *relationTableId=[NSString stringWithFormat:@"%@",model.relationTableId] ;
    
    NSString *fieldOfValue =[NSString stringWithFormat:@"t%li_au_%@_%@_%@", _tNum+1,relationTableId,showFieldArr,fieldId];
    return fieldOfValue;
}


#pragma mark - 前提条件设完修改前提条件
-(void)removeAlready:(EditModel *)model andTstring:(NSString *)tString andManyManyArr:(NSMutableArray *)manyManyArr andTnumBer:(NSInteger)tNum andSendMutDic:(NSMutableDictionary *)sendMutDic andEditDic:(NSMutableDictionary *)editDic andReturnBlock:(void (^)(NSMutableDictionary *, NSMutableArray *, EditModel *))cb{
    _sendMutDic = sendMutDic;
    _manyManyArr = manyManyArr;
    _EditDic = editDic;
    
    NSString *fieldRole =[NSString stringWithFormat:@"%@", model.fieldRole]; //[dic objectForKey:@"fieldRole"];
    NSInteger num =[fieldRole integerValue];
    
    if(num == 16){
        //        NSString *keyString = [self dataDicKeyGet:model orDefaultId:nil];
        if ([_sendMutDic valueForKey:model.montageName]) {
            [_sendMutDic removeObjectForKey:model.montageName];
            
            model.true_defaultShowValName = @"";
            model.true_defaultShowValName = @"";
            model.true_defaultShowValId = @"";
            
        }
        
    }else if (num == 17){//多选
    }else if(num == 20||num == 21||num == 22){ //*** 下拉树  + 弹层
        if(num == 22||num == 20){
            NSString *strK = [self getAlertKKey:model andTstring:tString];
            [_sendMutDic removeObjectForKey:strK];
            
            model.true_defaultShowValName = @"";
            model.true_defaultShowValId = @"";
            
        }
        else if(num == 21){
            NSString *field = [self getAlertMoreKKeyFieldOfValue:model andNum:tNum];
            NSString *fieldN = [self getAlertMoreKKeyFieldOfNum:model andTstring:tString];
            [_manyManyArr enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj valueForKey:field]) {
                    
                    model.true_defaultShowValName = @"";
                    model.true_defaultShowValName = @"";
                    model.true_defaultShowValId = @"";
                    
                    [_manyManyArr removeObject:obj];
                }else if([obj valueForKey:fieldN]){
                    [_manyManyArr removeObject:obj];
                }
            }];
        }
    }else{
        if ([_sendMutDic valueForKey:model.montageName]) {
            [_sendMutDic removeObjectForKey:model.montageName];
            
            model.true_defaultShowValName = @"";
            model.true_defaultShowValName = @"";
            model.true_defaultShowValId = @"";
            
        }
    }
    
    cb(_sendMutDic, _manyManyArr, model);
}


#pragma mark - 对象的内部 回调 ， 默认设置值 bing保证上传时有值不为空
-(void)whileFieldRowTypeIsspeciall:(EditModel *)model andTnum:(NSInteger )tNum andTstring:(NSString *)tString andSendMutDic:(NSMutableDictionary *)sendMutDic andManyMan:(NSMutableArray *)manyManArr andEditDetailDic:(NSMutableDictionary *)editDic andReturnBlock:(void (^)(NSMutableDictionary *, NSMutableArray *, NSDictionary *, EditModel *))cb{
    
    _sendMutDic = sendMutDic;
    _manyManyArr = manyManArr;
    _EditDic = editDic;
    
    NSString *fieldOfNum;
    NSString *fieldOfValue;
    
    
    NSString *fieldId;
    
    if(model.relationTableId!=nil){
        if(model.dialogField){
            
            fieldId= [NSString stringWithFormat:@"%@", model.dialogField];
        }else{
            fieldId= model.fieldId;
        }
    }else{
        fieldId= model.fieldId;
        
    }
#pragma mark - 班级作业选择学员返回错误，取返回的，不拼接
    fieldOfValue = [NSString stringWithFormat:@"%@", model.montageName1]; //[self getAlertMoreKKeyFieldOfValue:model andNum:tNum]; // t1
    //    fieldOfNum = [self getAlertMoreKKeyFieldOfNum:model];   // dz
    
    //    fieldOfNum = [NSString stringWithFormat:@"%@", model.montageName];   // dz
    fieldOfNum = [NSString stringWithFormat:@"%@", model.montageName]; //[self getAlertMoreKKeyFieldOfNum:model andTstring:tString];   // dz  // 还是有点问题等待陈蒙回来解决
    
    NSString *fieldRole =[NSString stringWithFormat:@"%@", model.fieldRole];
    NSInteger num =[fieldRole integerValue];
    if(num == 22||num == 20){
        
        
        NSString *pageId= [_EditDic objectForKey:@"startTurnPage"];
        //            NSString *tableId= [_dataDic objectForKey:@"tableId"];
        
        NSString *tableId= [_EditDic objectForKey:@"tableId"];
        
        NSString *baseString = [NSString stringWithFormat:@"_au_%@_%@_%@",tableId,pageId,fieldId];
        
        NSString *str;
        if(model.true_defaultShowValId){
            
            str=model.true_defaultShowValId;
        }else{
            str = @"";
        }
        [_sendMutDic setObject:[NSString stringWithFormat:@"%@", str] forKey:[KeyNameClass getKeyName:fieldRole andBaseString:baseString andTUseString:tString]];
        
        
        
    }else if(num == 21){
        if(_manyManyArr.count!=0){
            NSMutableArray *muArr = [[NSMutableArray alloc]initWithArray:_manyManyArr];
            
            for(NSDictionary *dict in _manyManyArr){
                if([[dict allKeys] containsObject:fieldOfValue]||[[dict allKeys] containsObject:fieldOfNum]){
                    [muArr removeObject:dict];
                }
            }
            _manyManyArr = muArr;
        }
        NSArray *arr;
        if(model.true_defaultShowValId){
            
            arr = [[NSString stringWithFormat:@"%@", model.true_defaultShowValId] componentsSeparatedByString:@","];
        }
        NSInteger arrNUm = [arr count];
        
        for(int i=0;i<arrNUm;i++){
            NSMutableDictionary *ddd = [[NSMutableDictionary alloc]init];
            [ddd setObject:[arr objectAtIndex:i] forKey:fieldOfValue];
            [_manyManyArr addObject:ddd];
        }
        NSMutableDictionary *dad = [[NSMutableDictionary alloc]init];
        [dad setObject:[NSString stringWithFormat:@"%ld",(long)arrNUm] forKey:fieldOfNum];
        [_manyManyArr addObject:dad];
        
    }
    
    //    manyManArr = _manyManyArr;
    //    sendMutDic = _sendMutDic;
    //    editDic = _EditDic;
    cb(_sendMutDic, _manyManyArr, _EditDic, model);
    NSLog(@"%@",_sendMutDic);
    
}


-(BOOL)textExit:(EditModel *)model andSendDic:(NSMutableDictionary *)send andMany:(NSMutableArray *)many andTnum:(NSInteger)tNum{
    
    NSString *fieldOfNum;
    NSString *fieldOfValue;
    
    fieldOfValue = [self getAlertMoreKKeyFieldOfValue:model andNum:tNum]; // t1
    fieldOfNum = [NSString stringWithFormat:@"%@", model.montageName];
    NSString *fieldRole =[NSString stringWithFormat:@"%@", model.fieldRole];
    NSInteger num =[fieldRole integerValue];
    
    if (num == 22||num == 20) {
        if ([send valueForKey:model.montageName]&&![[NSString stringWithFormat:@"%@", [send valueForKey:model.montageName]]isEqualToString:@""]) {
            return YES;
        }else{
            return NO;
        }
        
        
    }else if(num == 21){
        if(many.count!=0){
            
            for(NSDictionary *dict in many){
                if([[dict allKeys] containsObject:fieldOfValue]||[[dict allKeys] containsObject:fieldOfNum]){
                    
                    if([[NSString stringWithFormat:@"%@", [dict valueForKey:fieldOfNum]]isEqualToString:@"0"]){
                        return NO;
                    }
                    return YES;
                }
            }
            return NO;
        }
    }else{
        if ([send valueForKey:model.montageName]&&![[NSString stringWithFormat:@"%@", [send valueForKey:model.montageName]]isEqualToString:@""]) {
            return YES;
        }else{
            return NO;
        }
        
    }
    return YES;
}
#pragma mark - dic  to NSString
-(NSString *)fieldDicToString:(NSArray *)arr isAdd:(BOOL)is{
    NSString *str = @"";
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for(NSDictionary *dic in arr){
        NSArray *a1 =[dic allKeys];
        NSString *value =[dic objectForKey:a1[0]];
        NSString *equal = [NSString stringWithFormat:@"%@=%@",a1[0],value];
        [array addObject:equal];
    }
    if(!is){
        str =[array componentsJoinedByString:@"&"];
    }else{
        str =[array componentsJoinedByString:@"&&"];
        
    }
    return str;
}

#pragma mark - 格林尼治时间改所需的时间
+(NSString *)dateGlinToFormate:(NSString *)date andFromate:(NSString *)formate{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if(!formate){
        
        [formatter setDateFormat:DATEFORMATE];
    }else{
        [formatter setDateFormat:formate];
    }
    
    
    NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
    
    [formatter setLocale:enUSPOSIXLocale];
    NSString *dateLoca = [NSString stringWithFormat:@"%@",date];
    NSTimeInterval time=[dateLoca doubleValue]/1000.0;
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDate *dateBJTime = [[NSDate alloc]initWithTimeInterval:0 sinceDate:detaildate];
    
    
    NSString *destDateString = [formatter stringFromDate:dateBJTime];
    
    
    return destDateString;
}

#pragma mark - 规则生成
-(void)whileHaveTmpFieldId:(EditModel *)model1 andBaseString:(NSString *)baseString andValue:(NSString *)baseValue andButtonDic:(NSDictionary *)buttonDic andSendDic:(NSDictionary *)sendDic andNeedElseChangeArr:(NSMutableArray *)needElseChange andReturnBlock:(void (^)(NSMutableDictionary *, EditModel *, NSError *))cb{
    
    _buttonDic = buttonDic;
    
    for (EditModel *model2 in needElseChange) {
        
        if ([[NSString stringWithFormat:@"%@", model2.tmpFieldId]isEqualToString:[NSString stringWithFormat:@"%@", model1.fieldId]]) {
            
            NSLog(@"dic");
            
            
            [self requestNeedElse:model2 andBaseString:baseString andValue:baseValue andReturnBlock:cb];
        }
    }
}


-(void)requestNeedElse:(EditModel *)model andBaseString:(NSString *)baseString andValue:(NSString *)baseValue andReturnBlock:(void (^)(NSMutableDictionary *, EditModel *, NSError *))cb{
    
    
    
    NSMutableDictionary *parama = [[NSMutableDictionary alloc]init];
    [parama setObject:baseValue forKey:baseString];
    [parama setObject:[_buttonDic objectForKey:@"startTurnPage"] forKey:@"pageId"];
    [parama setObject:[_buttonDic valueForKey:@"tableId"] forKey:@"tableId"];
    
    NSString *requestStr = [NSString stringWithFormat:@"%@%@", getBase(), TMPREQUEST_URl];
    [NetRequest requestUrl:requestStr andParamas:parama andReturnBlock:^(NSData *data, NSError *error) {
        if (!error&&data!=nil) {
            
            NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@", obj);
            if (str) {
                
                model.true_defaultShowValName = str;
                
                
                [_sendMutDic setObject:model.true_defaultShowValName forKey:model.montageName];
                NSLog(@"%@",_sendMutDic);
                
                
                //                }
                
                cb(_sendMutDic, model, nil);
            }
            
            
        }else{
            
            cb(nil, nil, error);
            //            NSString *string = [NSString stringWithFormat:@"%@", error.localizedDescription];
            //            [self showLabelWithText:string];
        }
    }];
}


#pragma mark - textCanEdit
-(BOOL)textCanEdit:(NSInteger)num{
    if (num ==16||num == 14||num==28||num==26||num == 15||num==20||num==21||num==22||num==19) {
        return YES;
    }
    
    // default returrn NO
    return NO;
}

-(void)makeThePhone:(NSString *)teleNum{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"tel://", teleNum]]];
}


#pragma mark - 前提条件 截取
-(BOOL)needFilterHave:(EditModel *)model andManyManArr:(NSArray *)_mangManArr andCurrentPageDataArr:(NSArray *)_filedArr andTString:(NSString *)tString detailDic:(NSDictionary *)_detailDic andTableId:(NSString *)tableId andSendDic:(NSDictionary *)sendDic andReturnBlock:(void (^)(NSMutableDictionary *filterDic, NSString *error))filter{
    
    _filter = filter;
    
    NSString *needFilterStr = [NSString stringWithFormat:@"%@", model.needFilter];
    
    if ([needFilterStr length]>0&&![needFilterStr isEqualToString:@"(null)"]) {// 有前提条件下 开启前提条件模式
        [self needFilterTextHave:model andManyManArr:_mangManArr andCurrentPageDataArr:_filedArr andTString:tString detailDic:_detailDic andTableId:tableId andSendDic:sendDic];
    }
    
    
    // default YES
    return YES;
}

-(BOOL)needFilterTextHave:(EditModel *)model andManyManArr:(NSArray *)mangManArr andCurrentPageDataArr:(NSArray *)_filedArr andTString:(NSString *)tString detailDic:(NSDictionary *)_detailDic andTableId:(NSString *)tableId andSendDic:(NSDictionary *)sendDic{
    
    _manyManyArr = mangManArr;
    _sendMutDic = sendDic;
    
    
    
    NSString *needFilterStr = [NSString stringWithFormat:@"%@", model.needFilter]; // [dic valueForKey:@"needFilter"]
    
    NSArray *arrNeedFilters = [needFilterStr componentsSeparatedByString:@","];
    
    
    //*** needFilter take out with ":" to divOut
    //NSArray *arr;
    //*** many needFilters in one string take out the string we wang to use with "valueP"
    for (NSString *needFilterTakeOut in arrNeedFilters) {// 遍历多前提条件下的内容  多次处理
        
        [self needFilterTakeOut:needFilterTakeOut andFiledArr:_filedArr andTString:tString andDetailDic:_detailDic andTableId:tableId];
    }
    
    
    
    return YES;
}


#pragma mark - 有前提条件下 前提条件处理
-(void)needFilterTakeOut:(NSString *)needFilterTakeOut andFiledArr:(NSArray *)_filedArr andTString:(NSString *)tString andDetailDic:(NSDictionary *)_detailDic andTableId:(NSString *)tableId{
    
    NSMutableDictionary *selectSchoolKeyValueDic = [[NSMutableDictionary alloc]init];
    
    // 是否找到 前提条件
    BOOL findNeedFilter = NO;
    
    if ([needFilterTakeOut hasPrefix:@"valuePlug_"]) {
        
        NSString *selectSchoolKey = @"";
        NSString *selectSchoolValue = @"";
        
        selectSchoolKey = [self getSelectSchoolKey:needFilterTakeOut];
        
        
        NSString *stringVName; // 前提条件名称
        
        for (EditModel *model in _filedArr) { // for one
            
            
            // fieldRow 数据类型 进行 不同情况的 判断 处理
            NSString *fieldRoleMoment =[NSString stringWithFormat:@"%@", model.fieldRole];
            NSInteger numMoment =[fieldRoleMoment integerValue];
            
            
            // 当前数据的 数据 Id
            NSString *str = [NSString stringWithFormat:@"%@", model.fieldId];
            
            
            if ([str isEqualToString:[self getSelectSchoolNeedValue:needFilterTakeOut]]) { // if two   arr[1]
                // 当前条是 前提条件 所在 条 当前条 正确情况下 是前提条件 正确的 情况
                findNeedFilter = YES; // 找到了 其中一个 前提条件 接下来 验证 这个前提条件 是否是 需要的
                
                stringVName = model.fieldCnName;
                
                EditModel *dicForThis= model;
                NSString *fieldRole= [NSString stringWithFormat:@"%@", dicForThis.fieldRole];
                
                
                NSString *baseStringSend = [self createBaseStringSendForMoment:model];
                
                //*** 前提条件下 传送前提条件 当前 所输入的信息中  --- > 前提条件已输入
                if([_sendMutDic valueForKey:dicForThis.montageName]&& numMoment != 21){
                    
                    
                    
                    selectSchoolValue =[NSString stringWithFormat:@"%@", [_sendMutDic valueForKey:dicForThis.montageName]];// 有值 则取到当前值
                    
                    
                    
                    
                    
                    
                }else if(_manyManyArr && numMoment == 21){
                    
                    
                    
                    NSArray *valueArrs = [self fieldRow21Kye:model];
                    
                    selectSchoolValue = [NSString stringWithFormat:@"%@", [valueArrs componentsJoinedByString:@","]];
                    
                    if([_sendMutDic valueForKey:[KeyNameClass getKeyName:fieldRole andBaseString:baseStringSend andTUseString:tString]]&&selectSchoolValue.length<=0){
                        selectSchoolValue = [_sendMutDic valueForKey:[KeyNameClass getKeyName:fieldRole andBaseString:baseStringSend andTUseString:tString]];
                    }
                    
                    
                    
                }else{
                    
                    selectSchoolValue = @"";
                }
                
            }
            
            
            
        }
        
        //*** 关联添加显示
        if(!findNeedFilter){
            selectSchoolValue = [NSString stringWithFormat:@"%@",[_detailDic valueForKey:[NSString stringWithFormat:@"T_%@_0", tableId]]];
            
        }
        if(selectSchoolKey.length>0)[selectSchoolKeyValueDic setObject:selectSchoolValue forKey:selectSchoolKey];
        
        
        if (selectSchoolValue.length==0||!selectSchoolValue) {
            _filter(nil, stringVName);
        }else{
            _filter(selectSchoolKeyValueDic, nil);
            
        }
    }
    
    
}

#pragma mark - 前提条件 多值 取出 多值
-(NSArray *)fieldRow21Kye:(EditModel *)model{
    
    
    NSMutableArray *valueArrs = [[NSMutableArray alloc]init];
    
    for ( NSDictionary *dicManyArr in _manyManyArr) {
        
        NSString *relationTableId=[NSString stringWithFormat:@"%@",model.relationTableId];
        NSString *showFieldArr=[NSString stringWithFormat:@"%@",model.showFieldArr];
        NSString *dialogField=[NSString stringWithFormat:@"%@",model.dialogField];
        
        NSString *fieldOfValue =[NSString stringWithFormat:@"t1_au_%@_%@_%@",relationTableId,showFieldArr,dialogField];
        
        
        if([dicManyArr valueForKey:fieldOfValue]){
            [valueArrs addObject:[dicManyArr valueForKey:fieldOfValue]];
        }
        
    }
    return valueArrs;
}
#pragma mark - 有前提条件下 前提条件 字段的 键 因为他和 普通的键 不一样 所以单独封装一个
-(NSString *)createBaseStringSendForMoment:(EditModel *)model{
    EditModel *dicForThis= model;
    
    NSString *fieldId;
    if(dicForThis.relationTableId!=nil){
        fieldId= [NSString stringWithFormat:@"%@", dicForThis.dialogField];
        
    }else{
        fieldId= [NSString stringWithFormat:@"%@", dicForThis.fieldId];
        
    }
    if(fieldId == nil){
        fieldId = [NSString stringWithFormat:@"%@", dicForThis.fieldId];
    }
    NSString *pageId= [_buttonDic objectForKey:@"startTurnPage"];
    
    
    
    NSString *baseStringSend = [NSString stringWithFormat:@"_au_%@_%@_%@",model.relationTableId,pageId,fieldId];
    return baseStringSend;
}
#pragma mark - 前提条件数据的id
-(NSString *)getSelectSchoolKey:(NSString *)needFilterTakeOut{
    
    NSArray *arr;
    NSString *returnString;
    
    arr = [[NSString stringWithFormat:@"%@",  needFilterTakeOut]componentsSeparatedByString:@":"];
    
    if(arr.count>0)returnString = [NSString stringWithFormat:@"%@", arr[0]];
    
    return returnString;
    
}


#pragma mark - 前提条件值需要的
-(NSString *)getSelectSchoolNeedValue:(NSString *)needFilterTakeOut{
    
    NSArray *arr;
    NSString *returnString;
    
    arr = [[NSString stringWithFormat:@"%@",  needFilterTakeOut]componentsSeparatedByString:@":"];
    
    if(arr.count>1)returnString = [NSString stringWithFormat:@"%@", arr[1]];
    
    return returnString;
    
}

@end
